﻿@using Bootstrap
@using Orchard.UI.Resources
@using Bootstrap
@using Bootstrap.Services
@functions {
    // To support the layout classifaction below. Implementing as a razor function because we can, could otherwise be a Func<string[], string, string> in the code block following.
    //string CalcuShapeEvaluator.Classify(string[] zoneNames, string classNamePrefix) {
    //    var zoneCounter = 0;
    //    var zoneNumsFilled = string.Join("", zoneNames.Select(zoneName => { ++zoneCounter; return Model[zoneName] != null ? zoneCounter.ToString() : "";}).ToArray());
    //    return HasText(zoneNumsFilled) ? classNamePrefix + zoneNumsFilled : "";
    //}
    // I keep the above for posterity
    //We use ShapeEvaluator.Classify, I suggest you do too.
    
}
@{
    /* Global includes for the theme
    ***************************************************************/
    
    SetMeta("X-UA-Compatible", "IE=edge,chrome=1");
    Style.Require("bootstrap");
    //Jumpstart this biotch
    //Style.Include("site.css").AtHead();
    
    //jQuery
    Script.Require("jquery").AtFoot();
    
    //Examples of using Boostrap JS Components
    Script.Require("bootstrap.transition");
    Script.Require("bootstrap.alert");
    Script.Require("bootstrap.modal");
    Script.Require("bootstrap.dropdown");
    Script.Require("bootstrap.scrollspy");
    Script.Require("bootstrap.tab");
    Script.Require("bootstrap.tooltip");
    Script.Require("bootstrap.popover");
    Script.Require("bootstrap.button");
    Script.Require("bootstrap.collapse");
    Script.Require("bootstrap.carousel");
    Script.Require("bootstrap.typeahead");
    
    //Anything you gotta run everytime.
    //In this theme, its actually site.coffee
    Script.Include("site.js").AtFoot();
    
    /* Some useful shortcuts or settings
    ***************************************************************/

    //Func<dynamic, dynamic> Zone = x => Display(x); // Zone as an alias for Display to help make it obvious when we're displaying zones
    //Instead a "safish" Zone helper...
    Func<dynamic, dynamic> Zone = zone => zone != null ? Display(zone) : String.Empty;
    
    /* Layout classification based on filled zones
    ***************************************************************/

    //Add classes to the wrapper div to toggle aside widget zones on and off
    //These were all changed to ShapeEvaluator.Classify from CalcuClass
    var asideClass = ShapeEvaluator.Classify(Model, "aside", "AsideFirst", "AsideSecond"); // for aside-1, aside-2 or aside-12 if any of the aside zones are filled
    if (HasText(asideClass)) {
        Model.Classes.Add(asideClass);
    }

    //Add classes to the wrapper div to toggle tripel widget zones on and off
    var tripelClass = ShapeEvaluator.Classify(Model, "tripel", "TripelFirst", "TripelSecond", "TripelThird"); // for tripel-1, triple-2, etc. if any of the tripel zones are filled
    if (HasText(tripelClass)) {
        Model.Classes.Add(tripelClass);
    }

    //Add classes to the wrapper div to toggle quad widget zones on and off
    var footerQuadClass = ShapeEvaluator.Classify(Model, "quad", "FooterQuadFirst", "FooterQuadSecond", "FooterQuadThird", "FooterQuadFourth"); // for quad-1, quad-2, etc. if any of the quad zones are filled
    if (HasText(footerQuadClass)) {
        Model.Classes.Add(footerQuadClass);
    }

    //We need a little help from our friends.
    var settings = WorkContext.Resolve<IBootstrapSettingsService>();
    var columns = 0;
    if (!int.TryParse(settings.GetValue("gridColumns"), out columns)) {
        columns = 12;                                                                      
    }

    var fluid = false;
    bool.TryParse(settings.GetSetting("fluidLayout"), out fluid);
    
    //These will later be set with a setting....
    var containerClass =  fluid ? "container-fluid" : "container"; //We'll have a setting for fluid or fixed layouts.    
    var rowClass = fluid ? "row-fluid" : "row";
    
    int contentSpan = columns, asideSpan = 0;
    var asides = ShapeEvaluator.NotNull(Model, "AsideFirst", "AsideSecond");
    if (asides == 2)
    {
        contentSpan = (1/2) * columns;
        asideSpan = (1/4) * columns;
    }
    if (asides == 1)
    {
        contentSpan = (2/3) * columns;
        asideSpan = (1/3) * columns;
    }

    int tripelSpan = 0;
    bool anyTripel = false;
    var tripels = ShapeEvaluator.NotNull(Model, "TripelFirst", "TripelSecond", "TripelThird");
    if (tripels > 0) {
        anyTripel = true;
        tripelSpan = columns / tripels;
    }

    int quadSpan = 0;
    bool anyQuad = false;
    var quads = ShapeEvaluator.NotNull(Model, "FooterQuadFirst", "FooterQuadSecond", "FooterQuadThird", "FooterQuadForth");
    if (quads > 0) {
        anyQuad = true;
        quadSpan = columns / quads;
    }

    /* Inserting some ad hoc shapes
    ***************************************************************/

    WorkContext.Layout.Header.Add(New.Branding(), "5"); // Site name and link to the home page
    WorkContext.Layout.Footer.Add(New.BadgeOfHonor(), "5"); // Powered by Orchard
    //WorkContext.Layout.Footer.Add(New.User(), "10"); // Login and dashboard links

    /* Last bit of code to prep the layout wrapper
    ***************************************************************/

    Model.Id = "layout-wrapper";
    Model.Classes.Add(containerClass);
    if (bool.Parse(settings.GetSetting("fixedNavbar"))) {
        Model.Classes.Add("fixedNavbar");                                                    
    }
        
    var tag = Tag(Model, "div"); // using Tag so the layout div gets the classes, id and other attributes added to the Model
}

@*
Zones:
    Top:
        Header
        Navigation
    Before Body:
        Featured
        BeforeMain
    Body:
        AsideFirst
        Messages
        BeforeContent
        Content
        AfterContent
        AsideSecond
    Tripel:
        TripelFirst
        TripelSecond
        TripelThird
    FooterQuad:
        FooterQuadFirst
        FooterQuadSecond
        FooterQuadThird
        FooterQuadForth
    Footer:
        Footer
        
*@

@tag.StartElement
    @{
        var isFixed = true;
        
        bool.TryParse(settings.GetSetting("fixedNavbar"), out isFixed);
        var fixedNavbar = isFixed ? "navbar-fixed-top" : "";
    }
    <div class="@(rowClass) page-header">
        <div class="span8">
            @Zone(Model.Header)
        </div>
        <div class="span4">
            <div class="navbar @(fixedNavbar)">
                <div class="navbar-inner">
                    <div class="@(containerClass)">
                        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                          <span class="icon-bar"></span>
                          <span class="icon-bar"></span>
                          <span class="icon-bar"></span>
                        </a>
                        <div class="nav-collapse">
                            @Display(New.User())
                            @Zone(Model.Navigation)
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    @if (Model.Featured != null) {
    <div class="@(rowClass)">
        @Zone(Model.Featured)
    </div>
    }
    @if (Model.BeforeMain != null) {
    <div class="@(rowClass)">
        @Zone(Model.BeforeMain)
    </div>
    }
    
    <div class="@(rowClass)">
        @if (Model.AsideFirst != null) {
        <div class="span@(asideSpan)">
            @Zone(Model.AsideFirst)
        </div>                                  
        }
        <div class="span@(contentSpan)">
            @if (Model.Messages != null) {
            <div class="well">
                @Zone(Model.Messages)
            </div>
            }
            @if (Model.BeforeContent != null) {
            <div>
                @Zone(Model.BeforeContent)
            </div>
            }
            <div>
                @Zone(Model.Content)
            </div>
            @if (Model.AfterContent != null) {
            <div>
                @Zone(Model.AfterContent)
            </div>
            }
        </div>
        @if (Model.AsideSecond != null) {
        <div class="span@(asideSpan)">
            @Zone(Model.AsideSecond)
        </div> 
        }   
    </div>
    
    @if (anyTripel) {
    
        <div class="@(rowClass)">
            @if (Model.TripelFirst != null) {
            <div class="span@(tripelSpan)">
                @Zone(Model.TripelFirst)
            </div>                        
            }
            @if (Model.TripelSecond != null) {
            <div class="span@(tripelSpan)">
                @Zone(Model.TripelSecond)
            </div>                        
            }
            @if (Model.TripelThird != null) {
            <div class="span@(tripelSpan)">
                @Zone(Model.TripelThird)
            </div>                        
            }
        </div>
    
    }
    <footer>
        @if (anyQuad) {
        <div class="@(rowClass)">
            @if (Model.FooterQuadFirst != null) {
            <div class="@(quadSpan)">
                @Zone(Model.FooterQuadFirst)        
            </div>
            }
            @if (Model.FooterQuadSecond != null) {
            <div class="@(quadSpan)">
                @Zone(Model.FooterQuadSecond)        
            </div>
            }
            @if (Model.FooterQuadThird != null) {
            <div class="@(quadSpan)">
                @Zone(Model.FooterQuadThird)        
            </div>
            }
            @if (Model.FooterQuadForth != null) {
            <div class="@(quadSpan)">
                @Zone(Model.FooterQuadForth)        
            </div>
            }
        </div>    
        }
        @Zone(Model.Footer)
    </footer>
@tag.EndElement